//
//  LFBNetworkManager.m
//  loveFreshBeen
//
//  Created by Feng on 2017/7/9.
//  Copyright © 2017年 feng. All rights reserved.
//

#import "LFBNetworkManager.h"

#define HOME_DATA_URL @"http://iosapi.itcast.cn/loveBeen/focus.json.php"
#define NOVEL_SALE_URL @"http://iosapi.itcast.cn/loveBeen/firstSell.json.php"
#define APPLICATION_LAUNCH_ADVERT @"http://iosapi.itcast.cn/loveBeen/ad.json.php"
#define LIGHTNING_SUPERMARK @"http://iosapi.itcast.cn/loveBeen/supermarket.json.php"
#define NEW_KEYWORDS @"http://iosapi.itcast.cn/loveBeen/search.json.php"
#define SEARCH_DATA @"http://iosapi.itcast.cn/loveBeen/promotion.json.php"
#define MINE_INDENT @"http://iosapi.itcast.cn/loveBeen/MyOrders.json.php"
#define MINE_COUPON @"http://iosapi.itcast.cn/loveBeen/MyCoupon.json.php"
#define SYSTEM_INFO @"http://iosapi.itcast.cn/loveBeen/SystemMessage.json.php"
#define MINE_MESSAGE @"http://iosapi.itcast.cn/loveBeen/UserMessage.json.php"
#define RECEIVING_ADDRESS @"http://iosapi.itcast.cn/loveBeen/MyAdress.json.php"

@interface LFBNetworkManager ()

@property (nonatomic, strong) NSArray *array;

@end

@implementation LFBNetworkManager

+ (instancetype)sharedManager{
    
    static LFBNetworkManager *_manager;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        _manager = [[LFBNetworkManager alloc] init];
        
        _manager.requestSerializer = [[AFJSONRequestSerializer alloc] init];
        // 设置超时时间
        _manager.requestSerializer.timeoutInterval = 30.f;
        _manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/xml", @"text/plain", nil];
    });
    
    return _manager;
}

/**
 POST请求
 
 @param urlStr URL
 @param params 参数包装
 @param successBlock 成功回调
 @param failureBlock 失败回调
 */
+ (void)POST_RequestWithURL:(NSString *)urlStr parameters:(NSDictionary *)params success:(void(^)(id responseObject))successBlock failure:(void(^)(NSError *error))failureBlock{
    
    [[self sharedManager] POST:urlStr
                    parameters:params
                      progress:nil
                       success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                           
                           successBlock(responseObject);
                       }
                       failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                           failureBlock(error);
                       }];
}

/**
 GET请求
 
 @param urlStr URL
 @param params 参数包装
 @param successBlock 成功回调
 @param failureBlock 失败回调
 */
+ (void)GET_RequestWithURL:(NSString *)urlStr parameters:(NSDictionary *)params success:(void(^)(id responseObject))successBlock failure:(void(^)(NSError *error))failureBlock{
    
    [[self sharedManager] GET:urlStr
                   parameters:params
                     progress:nil
                      success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                          successBlock(responseObject);
                      }
                      failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                          failureBlock(error);
                      }];
}


/*>>>>>>>>>>>>>>>>>>>>>>>>>>>数据请求API<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
/**
 6.1.1首页数据请求
 
 @param resultBlock 数据回调
 */
+ (void)getHomeData:(void(^)(id responseObject))resultBlock failure:(void(^)(NSError *error))failureBlock{
    
    [LFBNetworkManager POST_RequestWithURL:HOME_DATA_URL
                             parameters:[self homeDataParams]
                                success:^(id responseObject) {
                                    resultBlock(responseObject);
                                }
                                failure:^(NSError *error) {
                                    failureBlock(error);
                                }];
}

/**
 6.1.2新鲜特卖数据请求
 
 @param resultBlock 返回数据回调
 */
+ (void)getNovelSALEData:(void(^)(id responseObject))resultBlock failure:(void(^)(NSError *error))failureBlock{
    
    [LFBNetworkManager POST_RequestWithURL:NOVEL_SALE_URL
                             parameters:[self novelSALEParams]
                                success:^(id responseObject) {
                                    resultBlock(responseObject);
                                }
                                failure:^(NSError *error) {
                                    failureBlock(error);
                                }];
}

/**
 6.1.3程序启动广告数据请求
 
 @param resultBlock 返回广告起请求数据
 */
+ (void)getApplicationLaunchAdvertData:(void(^)(id responseObject))resultBlock failure:(void(^)(NSError *error))failureBlock{
    
    [LFBNetworkManager POST_RequestWithURL:APPLICATION_LAUNCH_ADVERT
                             parameters:[self applicationLaunchAdvert]
                                success:^(id responseObject) {
                                    resultBlock(responseObject);
                                }
                                failure:^(NSError *error) {
                                    failureBlock(error);
                                }];
}

/**
 6.2.1闪电超市数据请求
 
 @param resultBlock 返回闪电超市请求数据
 */
+ (void)getLightningSupermarketData:(void(^)(id responseObject))resultBlock failure:(void(^)(NSError *error))failureBlock{
    
    [LFBNetworkManager POST_RequestWithURL:LIGHTNING_SUPERMARK
                             parameters:[self lightningSupermarketParams]
                                success:^(id responseObject) {
                                    resultBlock(responseObject);
                                }
                                failure:^(NSError *error) {
                                    failureBlock(error);
                                }];
}

/**
 6.3.1搜索最新关键词请求
 
 @param resultBlock 返回搜索最新关键字数据
 */
+ (void)searchNewKeyWords:(void(^)(id responseObject))resultBlock failure:(void(^)(NSError *error))failureBlock{
    [LFBNetworkManager POST_RequestWithURL:NEW_KEYWORDS
                             parameters:[self newKeyWordsParams]
                                success:^(id responseObject) {
                                    resultBlock(responseObject);
                                }
                                failure:^(NSError *error) {
                                    failureBlock(error);
                                }];
}

/**
 6.3.2搜索返回数据
 
 @param resultBlock 返回搜索数据
 */
+ (void)searchResponseData:(void(^)(id responseObject))resultBlock failure:(void(^)(NSError *error))failureBlock{
    [LFBNetworkManager POST_RequestWithURL:SEARCH_DATA parameters:[self searchDataParams]
                                success:^(id responseObject) {
                                    resultBlock(responseObject);
                                }
                                failure:^(NSError *error) {
                                    failureBlock(error);
                                }];
}

/**
 6.4.1获取订单信息
 
 @param resultBlock 返回我的订单信息
 */
+ (void)getMineIndent:(void(^)(id responseObject))resultBlock failure:(void(^)(NSError *error))failureBlock{
    [LFBNetworkManager POST_RequestWithURL:MINE_INDENT
                             parameters:[self mineIndentParams]
                                success:^(id responseObject) {
                                    resultBlock(responseObject);
                                }
                                failure:^(NSError *error) {
                                    failureBlock(error);
                                }];
}

/**
 6.4.2我的优惠券
 
 @param resultBlock 请求结果回调
 @param failureBlock 请求失败回调
 */
+ (void)mineCoupon:(void(^)(id responseObject))resultBlock failure:(void(^)(NSError *error))failureBlock{
    
    [LFBNetworkManager POST_RequestWithURL:MINE_COUPON
                             parameters:[self mineCoupon]
                                success:^(id responseObject) {
                                    resultBlock(responseObject);
                                }
                                failure:^(NSError *error) {
                                    failureBlock(error);
                                }];
    
}

/**
 6.4.3系统消息
 
 @param resultBlock 请求结果回调
 @param failureBlock 请求失败回调
 */
+ (void)systemInfo:(void(^)(id responseObject))resultBlock failure:(void(^)(NSError *error))failureBlock{
    
    [LFBNetworkManager POST_RequestWithURL:SYSTEM_INFO
                             parameters:[self systemInfoParams]
                                success:^(id responseObject) {
                                    resultBlock(responseObject);
                                }
                                failure:^(NSError *error) {
                                    failureBlock(error);
                                }];
}

/**
 6.4.4我的消息
 
 @param resultBlock 请求结果回调
 @param failureBlock 请求失败回调
 */
+ (void)mineMessage:(void(^)(id responseObject))resultBlock failure:(void(^)(NSError *error))failureBlock{
    
    [LFBNetworkManager POST_RequestWithURL:MINE_MESSAGE
                             parameters:[self mineMessage]
                                success:^(id responseObject) {
                                    resultBlock(responseObject);
                                }
                                failure:^(NSError *error) {
                                    failureBlock(error);
                                }];
}

/**
 6.4.5收货地址列表
 
 @param resultBlock 请求结果回调
 @param failureBlock 请求失败回调
 */
+ (void)receivingAddressList:(void(^)(id responseObject))resultBlock failure:(void(^)(NSError *error))failureBlock{
    
    [LFBNetworkManager POST_RequestWithURL:RECEIVING_ADDRESS
                             parameters:[self receivingAddressParams]
                                success:^(id responseObject) {
                                    resultBlock(responseObject);
                                }
                                failure:^(NSError *error) {
                                    failureBlock(error);
                                }];
}
/*>>>>>>>>>>>>>>>>>>>>>>>>参数包装<<<<<<<<<<<<<<<<<<<<<<<<*/
/**
 首页数据请求参数
 
 @return 参数字典
 */
+ (NSDictionary *)homeDataParams{
    return @{@"call":@(1)};
}

/**
 新鲜特卖请求参数
 
 @return 参数字典
 */
+ (NSDictionary *)novelSALEParams{
    return @{@"call":@(2)};
}

/**
 程序启动广告请求参数
 
 @return 参数字典
 */
+ (NSDictionary *)applicationLaunchAdvert{
    return @{@"call":@(7)};
}

/**
 闪电超市请求参数
 
 @return 参数字典
 */
+ (NSDictionary *)lightningSupermarketParams{
    return @{@"call":@(5)};
}

/**
 最新关键字参数
 
 @return 参数字典
 */
+ (NSDictionary *)newKeyWordsParams{
    return @{@"call":@(6)};
}

/**
 获取搜索结果参数
 
 @return 参数字典
 */
+ (NSDictionary *)searchDataParams{
    return @{@"call":@(8)};
}

/**
 我的订单信息参数
 
 @return 参数字典
 */
+ (NSDictionary *)mineIndentParams{
    return @{@"call":@(13)};
}

/**
 我的优惠券参数
 
 @return 参数字典
 */
+ (NSDictionary *)mineCoupon{
    return @{@"call":@(9)};
}

/**
 系统信息参数
 
 @return 参数字典
 */
+ (NSDictionary *)systemInfoParams{
    return @{@"call":@(10)};
}

/**
 我的信息参数
 
 @return 参数字典
 */
+ (NSDictionary *)mineMessage{
    return @{@"call":@(11)};
}

/**
 收货地址参数
 
 @return 参数字典
 */
+ (NSDictionary *)receivingAddressParams{
    return @{@"call":@(12)};
}


@end
